﻿using System.Collections.Generic;
using System.Windows.Input;
using Aeon.Emulator;

namespace Aeon.Presentation
{
    /// <summary>
    /// Contains extension methods for the Key type.
    /// </summary>
    public static class KeyExtensions
    {
        #region Constructors
        /// <summary>
        /// Initializes static fields on the KeyExtensions class.
        /// </summary>
        static KeyExtensions()
        {
            keyLookup.Add(Key.D0, Keys.Zero);
            keyLookup.Add(Key.D1, Keys.One);
            keyLookup.Add(Key.D2, Keys.Two);
            keyLookup.Add(Key.D3, Keys.Three);
            keyLookup.Add(Key.D4, Keys.Four);
            keyLookup.Add(Key.D5, Keys.Five);
            keyLookup.Add(Key.D6, Keys.Six);
            keyLookup.Add(Key.D7, Keys.Seven);
            keyLookup.Add(Key.D8, Keys.Eight);
            keyLookup.Add(Key.D9, Keys.Nine);
            keyLookup.Add(Key.A, Keys.A);
            keyLookup.Add(Key.B, Keys.B);
            keyLookup.Add(Key.C, Keys.C);
            keyLookup.Add(Key.D, Keys.D);
            keyLookup.Add(Key.E, Keys.E);
            keyLookup.Add(Key.F, Keys.F);
            keyLookup.Add(Key.G, Keys.G);
            keyLookup.Add(Key.H, Keys.H);
            keyLookup.Add(Key.I, Keys.I);
            keyLookup.Add(Key.J, Keys.J);
            keyLookup.Add(Key.K, Keys.K);
            keyLookup.Add(Key.L, Keys.L);
            keyLookup.Add(Key.M, Keys.M);
            keyLookup.Add(Key.N, Keys.N);
            keyLookup.Add(Key.O, Keys.O);
            keyLookup.Add(Key.P, Keys.P);
            keyLookup.Add(Key.Q, Keys.Q);
            keyLookup.Add(Key.R, Keys.R);
            keyLookup.Add(Key.S, Keys.S);
            keyLookup.Add(Key.T, Keys.T);
            keyLookup.Add(Key.U, Keys.U);
            keyLookup.Add(Key.V, Keys.V);
            keyLookup.Add(Key.W, Keys.W);
            keyLookup.Add(Key.X, Keys.X);
            keyLookup.Add(Key.Y, Keys.Y);
            keyLookup.Add(Key.Z, Keys.Z);
            keyLookup.Add(Key.Tab, Keys.Tab);
            keyLookup.Add(Key.CapsLock, Keys.CapsLock);
            keyLookup.Add(Key.LeftShift, Keys.LeftShift);
            keyLookup.Add(Key.LeftCtrl, Keys.Ctrl);
            keyLookup.Add(Key.LeftAlt, Keys.Alt);
            keyLookup.Add(Key.OemTilde, Keys.GraveApostrophe);
            keyLookup.Add(Key.Space, Keys.Space);
            keyLookup.Add(Key.OemOpenBrackets, Keys.OpenBracket);
            keyLookup.Add(Key.OemCloseBrackets, Keys.CloseBracket);
            keyLookup.Add(Key.OemSemicolon, Keys.Semicolon);
            keyLookup.Add(Key.OemQuotes, Keys.Apostrophe);
            keyLookup.Add(Key.OemComma, Keys.Comma);
            keyLookup.Add(Key.OemPeriod, Keys.Period);
            keyLookup.Add(Key.OemQuestion, Keys.Slash);
            keyLookup.Add(Key.OemMinus, Keys.Minus);
            keyLookup.Add(Key.Add, Keys.Equals);
            keyLookup.Add(Key.Back, Keys.Backspace);
            keyLookup.Add(Key.Oem5, Keys.Backslash);
            keyLookup.Add(Key.Enter, Keys.Enter);
            keyLookup.Add(Key.RightShift, Keys.RightShift);
            keyLookup.Add(Key.RightCtrl, Keys.Ctrl);
            keyLookup.Add(Key.RightAlt, Keys.Alt);
            keyLookup.Add(Key.Insert, Keys.Insert);
            keyLookup.Add(Key.Delete, Keys.Delete);
            keyLookup.Add(Key.Home, Keys.Home);
            keyLookup.Add(Key.End, Keys.End);
            keyLookup.Add(Key.PageUp, Keys.PageUp);
            keyLookup.Add(Key.PageDown, Keys.PageDown);
            keyLookup.Add(Key.Up, Keys.KeypadEight);
            keyLookup.Add(Key.Down, Keys.KeypadTwo);
            keyLookup.Add(Key.Left, Keys.KeypadFour);
            keyLookup.Add(Key.Right, Keys.KeypadSix);
            keyLookup.Add(Key.Escape, Keys.Esc);
            keyLookup.Add(Key.F1, Keys.F1);
            keyLookup.Add(Key.F2, Keys.F2);
            keyLookup.Add(Key.F3, Keys.F3);
            keyLookup.Add(Key.F4, Keys.F4);
            keyLookup.Add(Key.F5, Keys.F5);
            keyLookup.Add(Key.F6, Keys.F6);
            keyLookup.Add(Key.F7, Keys.F7);
            keyLookup.Add(Key.F8, Keys.F8);
            keyLookup.Add(Key.F9, Keys.F9);
            keyLookup.Add(Key.F10, Keys.F10);
            keyLookup.Add(Key.F11, Keys.F11);
            keyLookup.Add(Key.F12, Keys.F12);
        }
        #endregion

        #region Public Static Methods
        /// <summary>
        /// Converts a WPF Key into an emulator Keys.
        /// </summary>
        /// <param name="key">Key to convert.</param>
        /// <returns>Equivalent Keys value.</returns>
        public static Keys ToEmulatorKey(this Key key)
        {
            Keys value;
            if(keyLookup.TryGetValue(key, out value))
                return value;
            else
                return Keys.Null;
        }
        #endregion

        #region Private Static Fields
        private static readonly Dictionary<Key, Keys> keyLookup = new Dictionary<Key, Keys>();
        #endregion
    }
}
